import * as types from '../mutation';

const state = {
    selectedLoisticsList:[],
}

const actions = {
}

const mutations = {
    [types.PUSH_SELETED_ORDER](state,data){
        data.goodsCount = 1;
        state.selectedLoisticsList.push(data);
    },

    [types.ADD_SELETED_ORDER](state,data){
        let index = state.selectedLoisticsList.findIndex((d) => d.id == data.id);
        state.selectedLoisticsList[index].goodsCount++;
    },

    [types.SPLICE_SELETED_ORDER](state,data){
        if(data == 'all') {
            state.selectedLoisticsList = [];
            return;
        }
        let index = state.selectedLoisticsList.findIndex((d) => d.id == data.id);
        state.selectedLoisticsList[index].goodsCount--;
        if(state.selectedLoisticsList[index].goodsCount == 0) {
            state.selectedLoisticsList.splice(index, 1);
        }
    },
}

const getters = {
    totalPrice: (state) => {
        var temp = 0;
        for(var i = 0; i < state.selectedLoisticsList.length; i++) {
            temp += state.selectedLoisticsList[i].goodsPrice * state.selectedLoisticsList[i].goodsCount;
        }
        return temp.toFixed(2);
    },

    totalNum: () => {
        var temp = 0;
        for(var i = 0; i < state.selectedLoisticsList.length; i++) {
            temp += state.selectedLoisticsList[i].goodsCount * 1;
        }
        return temp;
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}